/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef ___OCVSTEREODISPLAY_HPP
#define ___OCVSTEREODISPLAY_HPP

#define GIMI_CLIENT_API (20000)
#include "gimi.h"
#include "ImageClient.hpp"
#include "JPEGDecoder.hpp"
#include "JPEGEncoder.hpp"
#include "cv.h"
#include "highgui.h"

#include <GL/gl.h>

class COCVStereoDisplay : public MaCI::Image::CImageClientCallback
{
public:
    
  static const int infoAreaHeight;
    
  COCVStereoDisplay();
    
  ~COCVStereoDisplay();
    

  /** Increase the undistortion offset (horizontal)
   *
   */
  void IncreaseUndistortionOffset(void)
  {
    iUndistortionOffset++;
    return;
  };

  /** Decrease the undistortion offset (horizontal)
   *
   */
  void DecreaseUndistortionOffset(void)
  {
    iUndistortionOffset--;
    return;
  };

  /** Get the distortion mode
   *
   */
  int GetUndistortionOffset(void)
  {
    return iUndistortionOffset;

  };

  /** Increase the undistortion offset (horizontal)
   *
   */
  void IncreaseAnaglyphOffset(void)
  {
    iAnaglyphOffset++;
    return;
  };

  /** Decrease the undistortion offset (horizontal)
   *
   */
  void DecreaseAnaglyphOffset(void)
  {
    iAnaglyphOffset--;
    return;
  };

  /** Get the distortion mode
   *
   */
  int GetAnaglyphOffset(void)
  {
    return iAnaglyphOffset;

  };


  /** Draw
   * 
   * Draws the image using a textured quad. Quad aspect ratio is same as image's.
   * The longer side of image will be set to 1 in gl coordinates. Quad lower 
   * left corner is placed at (0,0)
   */
  void Draw(int imageIndex=0);
    
  /** DisplayInfo
   * 
   * Define if a info field should be displayed in the picture
   */
  void DisplayInfo(bool info);
    
  /** DisplayWarnings
   * 
   * Define if warnings about slow frame rate or disconnect should be shown
   */
  void DisplayWarnings(bool warnings);
    
  /** GetWidth
   * 
   * Returns image width in pixels
   */
  int GetWidth();
    
  /** GetHeight
   * 
   * Returns image height in pixels
   */
  int GetHeight();
    
  /** Connect
   * 
   */
  bool Connect(gimi::GIMI *gimi, int gimnetID, int minor);
    
  void Disconnect();

  //This modes outputs stereo depth data
  void StereoDepthMode(char *aLeftRGBDataPtr, 
                       char *aRightRGBDataPtr);

  
  //This modes saves image samples to JPEG format.
  // Only saves if the calibration board is located
  void SaveSamplesMode(char* aLeftJPEGdataPtr, 
                       char *aRightJPEGdataPtr,
                       int aLeftJPEGSize,
                       int aRightJPEGSize);


  //This modes displays stereo anaglyph images
  void StereoAnaglyphMode(char* aLeftRGBDataPtr, 
                          char *aRightRGBDataPtr);


  bool BoardFound(IplImage* imageLeft,IplImage* imageRight);
  bool LoadCalibration(void);

  //callback for image client
  void OnImageDataEvent(const MaCI::Image::CImageData &aData, 
                        const unsigned int aDataSequence, 
                        MaCI::Image::CImageClient &aImageClient);
    

  bool IsConnected() {return connected;}
    
protected:
    
private:
  GLuint texture;
  double imageRatio;
  int imageWidth, imageHeight, texWidth, texHeight;

  //Horizontal offset for the undistortion function
  int iUndistortionOffset;
  //Horizontal offset for the anaglyph function
  int iAnaglyphOffset;

  //image cords on texture (whole texture is bigger than received image)
  double texCoordX,texCoordY;
  //quad dimensions
  double vertexX, vertexY;
  //if image size has changed
  bool sizeChanged;
    
  unsigned char *RGBData;
  ownMutexHandle imageMutex;
    
  bool connected;  //if connect has been tried
  bool opened;  //if connect was succesful
    
  bool displayInfo, displayWarnings;
  gim::time lastImageTime;
    
  MaCI::Common::TTimestamp lastTimeStamp;
    
  MaCI::Image::CImageClient *imageClient;
    
  void drawInfo(std::string message);
    
};

#endif
